/**
 * Copyright &copy; 2012-2016
 * <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights
 * reserved.
 */
package com.thinkgem.jeesite.modules.advertising.web;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.advertising.entity.AdvertisingPohoto;
import com.thinkgem.jeesite.modules.advertising.service.AdvertisingPohotoService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 广告图片管理Controller
 * 
 * @author JiangYongKang
 * @version 2016-12-15
 */
@Controller
@RequestMapping(value = "${adminPath}/advertising/advertisingPohoto")
public class AdvertisingPohotoController extends BaseController {

    @Autowired
    private AdvertisingPohotoService advertisingPohotoService;

    @ModelAttribute
    public AdvertisingPohoto get(@RequestParam(required = false) String id) {
        AdvertisingPohoto entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = advertisingPohotoService.get(id);
        }
        if (entity == null) {
            entity = new AdvertisingPohoto();
        }
        return entity;
    }

    @RequiresPermissions("advertising:advertisingPohoto:view")
    @RequestMapping(value = { "list", "" })
    public String list(AdvertisingPohoto advertisingPohoto,
            HttpServletRequest request, HttpServletResponse response,
            Model model) {
        Page<AdvertisingPohoto> page = advertisingPohotoService.findPage(
                new Page<AdvertisingPohoto>(request, response),
                advertisingPohoto);
        model.addAttribute("page", page);
        return "modules/advertising/advertisingPohotoList";
    }

    @RequiresPermissions("advertising:advertisingPohoto:view")
    @RequestMapping(value = "form")
    public String form(AdvertisingPohoto advertisingPohoto, Model model) {
        advertisingPohoto.setDictList(DictUtils.getDictList("advertising"));
        model.addAttribute("advertisingPohoto", advertisingPohoto);
        return "modules/advertising/advertisingPohotoForm";
    }

    @RequiresPermissions("advertising:advertisingPohoto:edit")
    @RequestMapping(value = "save")
    public String save(AdvertisingPohoto advertisingPohoto, Model model,
            RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, advertisingPohoto)) {
            return form(advertisingPohoto, model);
        }
        advertisingPohotoService.save(advertisingPohoto);
        addMessage(redirectAttributes, "保存广告图片成功");
        return "redirect:" + Global.getAdminPath()
                + "/advertising/advertisingPohoto/?repage";
    }

    @RequiresPermissions("advertising:advertisingPohoto:edit")
    @RequestMapping(value = "delete")
    public String delete(AdvertisingPohoto advertisingPohoto,
            RedirectAttributes redirectAttributes) {
        advertisingPohotoService.delete(advertisingPohoto);
        addMessage(redirectAttributes, "删除广告图片成功");
        return "redirect:" + Global.getAdminPath()
                + "/advertising/advertisingPohoto/?repage";
    }

    @RequestMapping(value = "upload")
    @ResponseBody
    public Object upload(MultipartHttpServletRequest request,
            HttpServletResponse response) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        Map<String, String> resultMap = new HashMap<String, String>();
        try {
            String dir = request.getSession().getServletContext().getRealPath("upload");
            File file = new File(dir);

            if (!file.exists()) {
                file.mkdirs();
            }

            String fileName = null;
            File uploadFile = null;

            if (fileMap.entrySet().iterator().hasNext()) {
                Map.Entry<String, MultipartFile> entity = fileMap.entrySet().iterator().next();
                MultipartFile mf = entity.getValue();
                fileName = mf.getOriginalFilename();
                uploadFile = new File(dir + File.separator + fileName);
                FileCopyUtils.copy(mf.getBytes(), uploadFile);
            }

            resultMap.put("fileName", fileName);
            resultMap.put("result", "success");
            resultMap.put("filePath", "\\jeesite\\upload" + File.separator + fileName);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "failure");
        }

        return resultMap;
    }
}